package com.mystudy.management.usermanagement.service;

import com.mystudy.management.usermanagement.dao.mappers.VisitorLimitMapper;
import com.mystudy.management.usermanagement.dao.mappers.VisitorMapper;
import com.mystudy.management.usermanagement.dao.pojo.Visitor;
import com.mystudy.management.usermanagement.dao.pojo.VisitorExample;
import com.mystudy.management.usermanagement.dao.pojo.VisitorLimit;
import com.mystudy.management.usermanagement.dao.pojo.VisitorLimitExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(
        isolation = Isolation.REPEATABLE_READ
)
public class Index2_2ServiceImpl implements Index2_2Service{

    @Autowired
    VisitorLimitMapper limitmapper;

    @Autowired
    VisitorMapper visitorMapper;

    //获取所有受控用户信息
    @Override
    public List<VisitorLimit> getAllLimits() {
        List<VisitorLimit> limits = limitmapper.selectByExample(null);
        if(limits.size() == 0){
            limits = new ArrayList<>();
        }
        return limits;
    }

    //根据Id查询
    @Override
    public List<VisitorLimit> getLimitsByCondition(Integer visitorId) {
        VisitorLimitExample example = new VisitorLimitExample();
        example.createCriteria().andUserIdEqualTo(visitorId);
        List<VisitorLimit> limits = this.limitmapper.selectByExample(example);
        if(limits.size() == 0){
            limits = new ArrayList<>();
        }
        return limits;
    }

    //根据日期范围查询
    @Override
    public List<VisitorLimit> getLimitsByCondition(Date startDate, Date endDate) {
        VisitorLimitExample example = new VisitorLimitExample();
        example.createCriteria().andEndDateBetween(startDate,endDate);
        List<VisitorLimit> limits = this.limitmapper.selectByExample(example);
        if(limits.size() == 0){
            limits = new ArrayList<>();
        }
        return limits;
    }


    //根据用户名查询
    @Override
    public List<VisitorLimit> getLimitsByCondition(String visitorName) {
        VisitorLimitExample example = new VisitorLimitExample();
        example.createCriteria().andUserNameEqualTo(visitorName);
        List<VisitorLimit> limits = this.limitmapper.selectByExample(example);
        if(limits.size() == 0){
            limits = new ArrayList<>();
        }
        return limits;
    }

    @Override
    public Map<String,Integer[]>  deleteLimitsByVisitorIds(Integer[] visitorIds) {
        Map<String,Integer[]> resultResponse  = new HashMap<>();
        resultResponse.put("Failed",new Integer[]{});
        for(Integer id: visitorIds){
            if(this.limitmapper.selectByPrimaryKey(id) != null ){
                this.limitmapper.deleteByPrimaryKey(id);
            }else {
                Integer[] failedField = (Integer[])resultResponse.get("Failed");
                Integer[] newFailedField = new Integer[failedField.length+1];
                newFailedField[failedField.length] = id;
                resultResponse.put("Failed",newFailedField);
            }
        }

        return resultResponse;
    }


    //批量修改事务
    @Override
    public void updateLimitsByVisitorIds(Map<Integer,Date[]> modifyMessages) {
        VisitorLimitMapper mapper = this.limitmapper;
        for (Map.Entry<Integer,Date[]> entry:modifyMessages.entrySet()){
            VisitorLimitExample example = new VisitorLimitExample();
            example.createCriteria().andUserIdEqualTo(entry.getKey());
            VisitorLimit visitorLimit = new VisitorLimit();
            visitorLimit.setStartDate(entry.getValue()[0]);
            visitorLimit.setEndDate(entry.getValue()[1]);
            mapper.updateByExampleSelective(visitorLimit,example);
        }
    }


    //获取所有未受控用户信息
    @Override
    public List<Visitor> getAllUnLimitVisitors() {
        List<VisitorLimit> limits = limitmapper.selectByExample(null);
        VisitorExample visitorExample = new VisitorExample();
        List<Integer> limitIds = new ArrayList<>();
        List<Visitor> freeVisitors ;
        for( VisitorLimit limitVisitor:limits){
            limitIds.add(limitVisitor.getUserId());
        }
        if(limitIds.size() != 0){
            visitorExample.createCriteria().andUserIdNotIn(limitIds);
            freeVisitors = visitorMapper.selectByExample(visitorExample);
        }else {
            freeVisitors = visitorMapper.selectByExample(null);
        }


        return freeVisitors;
    }

    //id为索引进行查询
    @Override
    public List<Visitor> getUnLimitVisitorByIndex(Integer index) {
        VisitorLimitExample limitExample = new VisitorLimitExample();
        limitExample.createCriteria().andUserIdEqualTo(index);
        List<VisitorLimit> limits = limitmapper.selectByExample(limitExample);
        VisitorExample visitorExample = new VisitorExample();
        List<Integer> limitIds = new ArrayList<>();
        List<Visitor> freeVisitors  = new ArrayList<>();
        for( VisitorLimit limitVisitor:limits){
            limitIds.add(limitVisitor.getUserId());
        }
        if(limitIds.size() == 0){
            visitorExample.createCriteria().andUserIdEqualTo(index);
            freeVisitors = visitorMapper.selectByExample(visitorExample);
        }


        return freeVisitors;
    }

    //用户名为索引进行查询
    @Override
    public List<Visitor> getUnLimitVisitorByIndex(String index) {
        VisitorLimitExample limitExample = new VisitorLimitExample();
        limitExample.createCriteria().andUserNameEqualTo(index);
        List<VisitorLimit> limits = limitmapper.selectByExample(limitExample);
        VisitorExample visitorExample = new VisitorExample();
        List<String> names = new ArrayList<>();
        List<Visitor> freeVisitors = new ArrayList<>();
        for (VisitorLimit limitVisitor : limits) {
            names.add(limitVisitor.getUserName());
        }
        if (names.size() == 0) {
            visitorExample.createCriteria().andUserNameEqualTo(index);
            freeVisitors = visitorMapper.selectByExample(visitorExample);
        }
        return freeVisitors;
    }


    //批量添加控制信息
    @Override
    public Integer[] addLimitsByVisitorIds(Map<Integer, Date[]> addMessages) {
        Integer[] resultCode = new Integer[]{0,0};
        for(Map.Entry<Integer,Date[]> entry:addMessages.entrySet()){
            Integer userId = entry.getKey();
            VisitorLimitExample example = new VisitorLimitExample();
            example.createCriteria().andUserIdEqualTo(userId);
            VisitorExample visitorExample = new VisitorExample();
            visitorExample.createCriteria().andUserIdEqualTo(userId);
            if(limitmapper.selectByExample(example).size() != 0){
                resultCode[1]++;
            }else if(visitorMapper.selectByExample(visitorExample).size() == 0) {
                resultCode[1]++;
            }else {
                VisitorLimitExample limitExample = new VisitorLimitExample();
                VisitorLimit limit = new VisitorLimit();
                limit.setStartDate(entry.getValue()[0]);
                limit.setEndDate(entry.getValue()[1]);
                limit.setUserId(userId);
                limit.setUserName(visitorMapper.selectByPrimaryKey(userId).getUserName());
                limitmapper.insertSelective(limit);
                resultCode[0]++;
            }
        }
        return resultCode;
    }
}
